package com.lazy.tcp;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

public class TelnetServerPipelineFactory implements ChannelPipelineFactory {
	
	/** 用户传入的channelHandler */
	protected ChannelHandler myHandler = null;

	/**
	 * @param myHandler 处理接受的数据的handler
	 */
	public TelnetServerPipelineFactory(ChannelHandler myHandler) {
		this.myHandler = myHandler;
	}

	@Override
	public ChannelPipeline getPipeline() throws Exception {
		// Create a default pipeline implementation.
		ChannelPipeline pipeline = pipeline();

        // Add the text line codec combination first,
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
                8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        
		//pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
		// pipeline.addLast("encoder", new ObjectEncoder());

		if (myHandler != null) {
			pipeline.addLast("myhandler", myHandler);
		}
		
		return pipeline;
	}
}
